// pages/personalCenter/components/text&ImageInputComponents.js
var network = require('../../../utils/requestManager.js');
Component({
  /**
     * 开启多组件支持
     * **/
  options: {
    multipleSlots: true,//启用多slot支持
    addGlobalClass: true//启用全局样式
  },
    /**
     * 组件的属性列表
     */
    properties: {
      list: {
          type: Array,
          value: {
            type: '',
            imagePath: ''
          },
          oberserver:function(new_val){
            console.log(new_val)
          }
      }
    },

    /**
     * 组件的初始数据
     */
    data: {
      imagePath: 'https://extend.qnssl.xuemei99.com/icon_za_changeqrc_add.png'
    },
    ///生命周期方法 附加时
    attached: function () {
        this.setData({
          list: [
            {
              type: "image",
              imagePath: "",
              text: "",
              first: undefined,
              last: true
            }
          ]
        })
    },
    /// 分离时32
    detached: function () { },

    /**
     * 组件的方法列表
     */
    methods: {
        ///选择图片
        clickToSelectImage(e) {
            let that = this
            let index = e.currentTarget.dataset.index
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                    console.log(res)
                    var tempFilePaths = res.tempFilePaths
                    var imagePath = tempFilePaths[0]
                    wx.showLoading({
                      title: '图片上传中...',
                    })
                    network.uploadimg({
                      path: tempFilePaths,
                      finished: function (uploadRes) {
                        wx.hideLoading()
                        console.log(uploadRes)
                        var list = that.data.list
                        var item = list[index]
                        item = {
                          type: item.type,
                          imagePath: imagePath,
                          imageName: uploadRes.images[0],
                          text: item.text,
                          first: item.first,
                          last: item.last
                        }
                        list[index] = item
                        that.setData({
                          list: list
                        })
                        that.triggerEvent('contentDidChanged', { 'list': that.data.list })
                      }
                    })
                    
                }
            })

        },
        ///输入文本
        bindInput(e) {
            let text = e.detail.value
            let index = e.currentTarget.dataset.index
            var list = this.data.list
            var item = list[index]
            item = {
                type: item.type,
                imagePath: item.imagePath,
                imageName: item.imageName,
                text: text,
                first: item.first,
                last: item.last
            }

            list[index] = item

            this.setData({
                list: list
            })
            this.triggerEvent('contentDidChanged', { 'list': this.data.list })

        },
        ///点击添加
        clickToAdd(e) {
            let typeStr = e.currentTarget.dataset.type
            let index = e.currentTarget.dataset.index
            console.log(index + ' ' + typeStr)
            var descList = this.data.list
            var item = {
                type: typeStr,
                imagePath: "",
                imageName:'',
                text: '',
                first: false
            }
            descList.splice(index + 1, 0, item)
            checkedTheStatusWith(this)
        },
        ///点击删除
        clickToDelete(e) {
            let that = this
            wx.showModal({
                title: '提示',
                content: '确认删除吗',
                success(res) {
                    if (res.confirm) {
                        let index = e.currentTarget.dataset.index
                        console.log('删除')
                        var descList = that.data.list
                        descList.splice(index, 1);
                        checkedTheStatusWith(that)
                    }
                    else if (res.cancel) {
                        console.log('取消')
                    }
                }
            })
        },
        ///改变排序
        clickToChangeRank(e) {
            let direction = e.currentTarget.dataset.direction
            let index = e.currentTarget.dataset.index
            changeRankofTheList(index, direction, this)
        }
    }
})
function changeRankofTheList(index, direction, that) {
    let list = that.data.list
    var item = list[index];
    list.splice(index, 1)
    if (direction == 'down') {
        list.splice(index + 1, 0, item)
    }
    else if (direction == 'up') {
        list.splice(index - 1, 0, item)
    }
    checkedTheStatusWith(that)

}
function checkedTheStatusWith(that) {
    var list = that.data.list
    for (let i = 0; i < list.length; i++) {
        list[i].first = i === 0
        list[i].last = i === list.length - 1
    }
    if (list.length == 1) {
        list[0].first = undefined
    }
    that.setData({
        list: list
    })
    that.triggerEvent('contentDidChanged', { 'list': that.data.list })

}